//
//  YPGuidanceViewController.m
//  ChengHuiTong
//
//  Created by KiVen on 15/12/2.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "YPGuidanceViewController.h"
#import "MainTabbarViewController.h"
#import "StyledPageControl.h"
#import "CALayer+Anim.h"
@interface YPGuidanceViewController ()<UIScrollViewDelegate>{
    
    StyledPageControl *_pageControl;
    UIScrollView* _launchScrollView;

}

@end

@implementation YPGuidanceViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    
    [RequestModel getBaseUrlSuccessBlock:^(id obj) {
        if ([obj[@"status"] intValue]==0) {
            
            NSDictionary*dic=obj[@"data"];
            
            NSString*api_url=dic[@"api_url"];
            
            _hostUrl=api_url;
            
            NSLog(@"%@",api_url);
            
            YPShareAccountInfo*share=[YPShareAccountInfo shareAccountInfo];
            
            share.hostUrl=api_url;
            
            [share saveData];
            
        }
        
    } failBlock:^(id obj) {

        if (![YPShareAccountInfo shareAccountInfo].hostUrl) {
            
            [YPShareAccountInfo shareAccountInfo].hostUrl=@"https://www.chenghuitong.net";
            
            [[YPShareAccountInfo shareAccountInfo] saveData];
            
        }
    }];

    //添加scrollerview
    [self addScrollerView];
    //    添加PageController
    [self addPage];
    //   添加图片
    [self addImgView];

}

- (void)addScrollerView{
    
    _launchScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _launchScrollView.delegate= self;
    _launchScrollView.contentSize = CGSizeMake(SCREEN_WIDTH* 3, SCREEN_HEIGHT);
    _launchScrollView.pagingEnabled = YES;
    _launchScrollView.showsHorizontalScrollIndicator = NO;
    _launchScrollView.showsVerticalScrollIndicator = NO;
    _launchScrollView.bounces = NO;
    [self.view addSubview:_launchScrollView];
    
}

- (void)addImgView{
    
    for (int i = 0 ; i < 3; i ++)
    {
        UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i+400;
        if (iPhone4) {
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guidance%d_4.png",i]];
        }else if (iPhone5) {
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guidance%d_5.png",i]];
        }else if (iPhone6) {
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guidance%d_6.png",i]];
        }else{
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guidance%d_p.png",i]];
        }
        
        if (i == 2) {
            
            UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
            startButton.frame = CGRectMake((SCREEN_WIDTH - 117)/2.f, _pageControl.top - 54, 117, 39);
            [startButton setTitle:@"立即体验" forState:0];
            [startButton setBackgroundColor:UIColorFromString(@"#ffa200")];
            [startButton setTitleColor:[UIColor whiteColor] forState:0];
            startButton.titleLabel.font=[UIFont systemFontOfSize:16];
            ViewBorderRadius(startButton, startButton.height/2.f, 1, [UIColor whiteColor]);
            [startButton addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:startButton];
        }
        
        [_launchScrollView addSubview:imageView];
    }
}

- (void)addPage{
    
    //分页控制器
    _pageControl = [[StyledPageControl alloc]init];
    CGSize size = self.view.frame.size;
    _pageControl.center = CGPointMake(size.width *0.5, size.height *0.94);
    _pageControl.bounds = CGRectMake(0, 0, 150, 15);
    _pageControl.numberOfPages=_launchScrollView.contentSize.width/SCREEN_WIDTH;
    _pageControl.pageControlStyle = PageControlStyleStrokedCircle;
    _pageControl.userInteractionEnabled = NO;
    _pageControl.strokeSelectedColor = kColor_AppNavColor;
    _pageControl.coreSelectedColor = kColor_AppNavColor;
    _pageControl.strokeNormalColor = kColor_AppNavColor;
    _pageControl.strokeWidth = 1.5;
    _pageControl.diameter = 10;
    [self.view addSubview:_pageControl];
    
    
}

- (void)start:(UIButton *)sender
{
    // 控制器的view是延迟加载：需要显示的\用到的时候才会加载
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    
    [window.layer moveIn];
    // 显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    MainTabbarViewController * mainTabbar = [[MainTabbarViewController alloc]init];
    
    window.rootViewController = mainTabbar;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    _pageControl.currentPage = scrollView.contentOffset.x/SCREEN_WIDTH;
    
}

@end
